package com.nine.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.nine.entity.Userinfo;

import java.util.Base64;

/**
 * @author ：婷儿
 * @date ：Created in 2022/6/25 15:19
 * @description：密码加盐加密
 * @modified By：
 * @version: 1.0
 */
public class MyTools {
    public Userinfo makePasswordSalt(Userinfo userinfo){
        String passwordLod = userinfo.getPassword();
        //随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        String salt= Base64.getEncoder().encodeToString(key);

//构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);

//加密为16进制表示
        String encryptHex = aes.encryptHex(userinfo.getPassword());
        userinfo.setPassword(encryptHex);
        userinfo.setSalt(salt);
        return userinfo;
    }
    public static String makePassword(String password,String salt){
        // 构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, cn.hutool.core.codec.Base64.decode(salt));
        // 加密为16进制表示
        String newPassword = aes.encryptHex(password);
        return newPassword;
    }
}
